import ToolActionApi from "@/tool/http/tool-action.js";
import toolWaitObj from "@/tool/tool-wait-obj.js";

let dictTableColCache = {};
let dictTableColRes = [];
let dictLogic = undefined;

export default {
  async dictTableCol(dsId, tableName) {
    if (
      dictTableColCache.dsId === dsId &&
      dictTableColCache.tableName === tableName
    ) {
      await toolWaitObj.wait(() => dictTableColRes.length > 0);
      return dictTableColRes;
    }
    dictTableColCache.dsId = dsId;
    dictTableColCache.tableName = tableName;
    let res = await ToolActionApi.run("action-table-info", tableName, {
      _dsId: dsId,
    });
    return (dictTableColRes = res.map((item) => ({
      name: `${item.memo}(${item.name})`,
      val: item.name,
    })));
  },
  async dictLogic() {
    if (!dictLogic) {
      dictLogic = await ToolActionApi.enumDict("logic");
    }
    console.log(dictLogic);
    return dictLogic;
  },
};
